﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using EntityObjects;
namespace PresentationLayer.View
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        RaisePurchaseOrderControl c;
        string ItemId;
        static string flag = "false";
        List<PurchaseOrderDetails> lst,lst1;
        List<PurchaseOrderDetails> allLst;
        static public string fstIndex = "fst";
        static int count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            c = new RaisePurchaseOrderControl();
            if (!IsPostBack || Session["purchase"] != null)
            {                
                allLst = new List<PurchaseOrderDetails>();
                if (c.getReorderLvlPurchaseOrder() != null)
                {
                    ViewState[fstIndex] = c.getReorderLvlPurchaseOrder();
                    foo.DataSource = ViewState[fstIndex];
                    foo.DataBind();
                    btnGPurchaseOrder.Enabled = false;
                    tot.Value = "First";
                    count++;
                }
                else lblNull.Visible = true;
            }                                  
        }
        protected void foo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Purchase" || e.CommandName == "Add")
            {               
            ItemId = e.CommandArgument.ToString();          
            Catalogue cat = c.getReorderLvlPurchaseOrderById(ItemId);
            lblCurrentStock.Text = cat.TotalQty.ToString();
            lblItemCode.Text =ItemId;
            lblItemName.Text = cat.Description;
            lblReorderQty.Text = cat.ReOrderQty.ToString();
            foo0.DataSource = c.getSupplierTender(ItemId);
            foo0.DataBind();          
                foreach (GridViewRow gr in foo0.Rows) {                    
                    List<PurchaseOrderDetails> poLstTmp=new List<PurchaseOrderDetails>();
                     poLstTmp= (List<PurchaseOrderDetails>)ViewState[gr.Cells[0].Text + foo.PageIndex];
                    if(poLstTmp!=null){
                        foreach (PurchaseOrderDetails po in poLstTmp)
                        {
                            if (po.ItemId == ItemId)
                            {
                                Label amt = (Label)gr.Cells[3].FindControl("Amount");
                                TextBox qty = (TextBox)gr.Cells[2].FindControl("qty");
                                amt.Text = po.Amount.ToString();
                                qty.Text = po.OrderQty.ToString();
                            }
                        }
                    }                
            }         
            btnOrder.Enabled = false;
            addOrderList.Visible = true;
            flag = "false";      
                
            }
        }
        
        protected void qtyText_Changed(object sender, EventArgs e)
        {            
            TextBox thisTextBox = (TextBox)sender;
            GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent;
            GridView thisGridView = (GridView)thisGridViewRow.Parent.Parent;
            int r = thisGridViewRow.RowIndex;
            Label lblInvalidQty = (Label)thisGridViewRow.Cells[2].FindControl("lblInvalidQty");          
            if (!thisTextBox.Text.Equals("") && !(thisTextBox.Text.Equals("0")))
            {          
                    int quantity = Convert.ToInt32(thisTextBox.Text.Trim());
                    int price = Convert.ToInt32(foo0.DataKeys[thisGridViewRow.RowIndex].Value);
                    Label l = (Label)thisGridViewRow.Cells[3].FindControl("Amount");
                    double t = (price * quantity);
                    int total=0;
                    l.Text = t.ToString();
                    foreach(GridViewRow gvr in thisGridView.Rows){
                        TextBox lblTextQty = (TextBox)gvr.Cells[2].FindControl("qty");
                        if (lblTextQty.Text != null && lblTextQty.Text != "") total += Convert.ToInt32(lblTextQty.Text);                         
                    }
                    tot.Value = total.ToString();                                
                    btnOrder.Enabled = true;              
            }
        }

        protected void btnOrder_Click(object sender, EventArgs e)
        {
            int r;
            if (Convert.ToInt16(tot.Value.ToString()) >= Convert.ToInt16(lblReorderQty.Text.ToString()))
            {
                r = foo.PageIndex;
                foreach (GridViewRow gvr in foo0.Rows)
                {
                    string supName = gvr.Cells[0].Text;    //get data from BoundField in each row
                    string qty = ((TextBox)gvr.FindControl("qty")).Text;    //get data from control in TemplateField in each row                      

                    PurchaseOrderDetails tmp = new PurchaseOrderDetails();
                    if (qty != "")
                    {                          
                        tmp.ItemId = lblItemCode.Text;
                        tmp.OrderQty = Convert.ToInt16(qty);
                        tmp.Price = Convert.ToDouble(gvr.Cells[1].Text);
                        tmp.Amount = Convert.ToDouble(gvr.Cells[1].Text) * Convert.ToInt16(qty);

                        if (Session[supName] == null) lst = new List<PurchaseOrderDetails>();
                        else lst = (List<PurchaseOrderDetails>)Session[supName];

                        if (ViewState[supName + r] == null) lst1 = new List<PurchaseOrderDetails>();
                        if (ViewState[supName+r] != null)lst1 = (List<PurchaseOrderDetails>)ViewState[supName+r];  
                    
                        if (!lst.Contains(tmp))
                        {
                            foreach (PurchaseOrderDetails p in lst) {
                                if (p.ItemId == tmp.ItemId) {
                                    lst.Remove(p); break;                                    
                                }
                            }
                            lst.Add(tmp);
                            Session[supName] = lst;
                            ViewState[supName + r] = lst1;
                        }
                        if (!lst1.Contains(tmp))
                        {
                            foreach (PurchaseOrderDetails p in lst1)
                            {
                                if (p.ItemId == tmp.ItemId)
                                {
                                    lst1.Remove(p); break;
                                }
                            }
                            lst1.Add(tmp);                            
                            ViewState[supName + r] = lst1;
                        }
                        err.Text = " ";
                        flag = "true";            
                        btnOrder.Enabled = false;
                        foreach (GridViewRow gr in foo.Rows) { 
                            LinkButton lblPur=(LinkButton) gr.Cells[6].FindControl("Purchase");
                            if (lblPur.CommandArgument.ToString() == tmp.ItemId) {
                                ViewState[gr.RowIndex.ToString() + r] = "Added To Order List";
                                lblPur.Text = (String)ViewState[gr.RowIndex.ToString() + r];                             
                                lblPur.CommandName = "Add";
                            }
                        }
                        addOrderList.Visible = false;
                        btnGPurchaseOrder.Enabled = true;
                    }
                }
            }
            else if (Convert.ToInt16(tot.Value.ToString()) <= Convert.ToInt16(lblReorderQty.Text.ToString())) err.Text = "Total Qty Doesn't Match With Reorder Qty";
        }

        protected void btnGPurchaseOrder_Click1(object sender, EventArgs e)
        {
            Server.Transfer("PurchaseOrderForm.aspx");
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            addOrderList.Visible = false;

        }

        protected void foo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            foo.PageIndex = e.NewPageIndex;
            Bind();
        }
        private void Bind()
        {
            int i=foo.PageIndex;
            foo.DataSource = ViewState[fstIndex];
            foo.DataBind();
            foreach(GridViewRow gvr in foo.Rows){              
                if (ViewState[gvr.RowIndex.ToString() + foo.PageIndex] != null)
                {
                    LinkButton lBtn = (LinkButton)gvr.Cells[6].FindControl("Purchase");
                    lBtn.Text = ViewState[gvr.RowIndex.ToString()+foo.PageIndex].ToString();
                    if (lBtn.Text == "Purchased")
                    {
                        lBtn.Enabled = false;
                        
                    }
                   
                }              
            }
            addOrderList.Visible = false;
        }

        protected void foo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow gvr in foo.Rows)
            {
                string id = gvr.Cells[1].Text.ToString();
                bool flag = c.FindPurchasedItem(id);
                LinkButton lBtn = (LinkButton)gvr.Cells[6].FindControl("Purchase");
                if (flag == true)
                {
                    if (ViewState[gvr.RowIndex.ToString() + foo.PageIndex] == null)
                    {
                        lBtn.Enabled = false;
                        ViewState[gvr.RowIndex.ToString() + foo.PageIndex] = "Purchased";
                        lBtn.Text = (String)ViewState[gvr.RowIndex.ToString() + foo.PageIndex].ToString();

                    }
                    else lBtn.Text = (String)ViewState[gvr.RowIndex.ToString() + foo.PageIndex];
                }

            }
        }
        
    }
}